package com.pwr.app.ui;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.pwr.app.characteristics.CharacteristicsEnum;

public class LearningWindow extends JDialog implements ActionListener, ListSelectionListener {

	private static final long serialVersionUID = 1L;
	JDialog jDialog;
	String[] options = { "10", "20", "30", "40", "50", "100", "150", "200", "500", "1000" };
	JCheckBox fourier = new JCheckBox("Fourier");
	JCheckBox hisotgram = new JCheckBox("Histogram");
	JCheckBox density = new JCheckBox("Density");
	JCheckBox huMoments = new JCheckBox("Hu moments");

	JTextField methodsField = new JTextField("Choose characteristisc:");
	JTextField iterationsField = new JTextField("Choose number characteristics");
	JList<String> listField = new JList<String>();
	JButton okButton = new JButton("OK");
	JButton cancelButton = new JButton("Cancel");
	JScrollPane listScroller;
	Map<CharacteristicsEnum, Boolean> characteristicsUsed;

	private int method;
	private int iterations;

	public int getMethod() {
		return method;
	}

	public void setMethod(int method) {
		this.method = method;
	}

	public int getIterations() {
		return iterations;
	}

	public void setIterations(int iterations) {
		this.iterations = iterations;
	}

	public LearningWindow(JFrame parent, String title, String message, Map<CharacteristicsEnum, Boolean> characteristicsUsed) {

		super(parent, title, true);
		Dimension parentSize = parent.getSize();
		Point p = parent.getLocation();
		this.characteristicsUsed = characteristicsUsed;
		setLocation(p.x + parentSize.width / 4, p.y + parentSize.height / 4);
		CreateAndShowGUI(parent, title);

	}

	private void CreateAndShowGUI(JFrame parent, String title) {
		jDialog = new JDialog(parent, title); // Creates dialog
		jDialog.setModal(true); // Means it will wait
		jDialog.add(CreatePanel()); // Add your panel
		jDialog.pack(); // Set size (probably want this relating to your panel
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		jDialog.setLocation(dim.width / 2 - jDialog.getWidth() / 2, dim.height / 2 - jDialog.getHeight() / 2);
		jDialog.setVisible(true);
		jDialog.setResizable(false);
		jDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

	}

	private void exit() {
		jDialog.dispose();
	}

	private JPanel CreatePanel() {
		JPanel TotalGUI = new JPanel(new GridLayout(2, 2));

		JPanel radioButtonPanel = new JPanel(new GridLayout(3, 1));

		fourier.addActionListener(this);
		hisotgram.addActionListener(this);
		density.addActionListener(this);
		huMoments.addActionListener(this);
		fourier.setSelected(true);
		radioButtonPanel.add(fourier);
		radioButtonPanel.add(hisotgram);
		radioButtonPanel.add(density);
		radioButtonPanel.add(huMoments);

		listField = new JList<String>(prepareListElements());
		listField.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		listField.setLayoutOrientation(JList.VERTICAL);
		listField.addListSelectionListener(this);
		listField.setSelectedIndex(0);
		listScroller = new JScrollPane(listField);
		listScroller.setPreferredSize(new Dimension(250, 85));

		methodsField.setEditable(false);
		iterationsField.setEditable(false);
		okButton.addActionListener(this);
		cancelButton.addActionListener(this);
		TotalGUI.add(methodsField);
		TotalGUI.add(radioButtonPanel);
		// TotalGUI.add(iterationsField);
		// TotalGUI.add(listScroller);
		TotalGUI.add(okButton);
		TotalGUI.add(cancelButton);

		return TotalGUI;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		Object source = e.getSource();

		if (source == density) {
			method = 1;
			characteristicsUsed.put(CharacteristicsEnum.Density, !characteristicsUsed.get(CharacteristicsEnum.Density));
		}
		if (source == fourier) {
			method = 2;
			characteristicsUsed.put(CharacteristicsEnum.Fourier, !characteristicsUsed.get(CharacteristicsEnum.Fourier));

		} else if (source == hisotgram) {
			method = 3;
			characteristicsUsed.put(CharacteristicsEnum.Histogram, !characteristicsUsed.get(CharacteristicsEnum.Histogram));

		} else if (source == huMoments) {
			method = 4;
			characteristicsUsed.put(CharacteristicsEnum.HuMoment, !characteristicsUsed.get(CharacteristicsEnum.HuMoment));

		} else if (source == okButton)
			exit();
		else if (source == cancelButton)
			exit();

	}

	private ListModel<String> prepareListElements() {

		ListModel<String> listModel = new DefaultListModel<String>();
		for (String string : options) {
			((DefaultListModel<String>) listModel).addElement(string);
		}
		return listModel;
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		if (e.getValueIsAdjusting() == false) {

			if (listField.getSelectedIndex() == -1) {

			} else {
				iterations = Integer.parseInt(options[listField.getSelectedIndex()]);
			}
		}

	}

}
